package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.SpecMapper;
import com.itheima.pojo.Spec;
import com.itheima.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private SpecMapper specMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        //根据条件查询
        Spec spec = new Spec();
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())) {
            spec.setName(queryPageBean.getQueryString());
        }
        List<Spec> brands = specMapper.select(spec);

        PageInfo pageInfo = new PageInfo(brands);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public List<Map> findAllSpecWithOptions(Integer category3id) {
        List<Map> resultList = new ArrayList<Map>();

        List<Integer> specIds = specMapper.findSpecIdsByCategoryId(category3id);

        if (specIds != null && specIds.size() > 0) {
            Example example = new Example(Spec.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("id", specIds);

            List<Spec> specs = specMapper.selectByExample(example);

            if (specs != null) {
                for (Spec spec : specs) {
                    Map map = new HashMap();
                    map.put(spec.getName(), spec.getOptions().split(","));
                    resultList.add(map);
                }
            }
        }


        return resultList;
    }

    @Override
    public Result handleAdd(Spec spec) {
        if (spec.getName() == null || spec.getOptions() == null || spec.getSeq() == null) {
            return new Result(false, "新增失败!");
        } else {
            specMapper.handleAdd(spec);
            return new Result(true, "新增成功!");
        }
    }

    @Override
    public Result handleDelete(Integer id) {
        Integer i = specMapper.handleDelete(id);
        if (i > 0) {
            return new Result(true, "删除成功!");
        } else {
            return new Result(false, "删除失败!");
        }

    }

    @Override
    public Result findById(Integer id) {
        Spec spec = specMapper.findById(id);
        if (spec!=null) {
            return new Result(true,"",spec);
        }else {
            return new Result(false,"查询失败!",null);
        }
    }

    @Override
    public Result update(Spec spec) {
        Integer i = specMapper.update(spec);
        if (i > 0) {
            return new Result(true, "修改成功!");
        } else {
            return new Result(false, "删除失败!");
        }
    }
}
